﻿using System;
using System.Linq;
using System.Windows.Input;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace Avivo.Tools.Sprite.Models
{
	public class SpriteModel : ModelBase
	{
		#region Members
		private int index;
		private string name;
		private string content;
		private string template;
		private ICommand addSpriteCommand;
		private ICommand removeSpriteCommand;
		private SpriteViewModel parent;
		#endregion

		#region Constructor
		public SpriteModel()
		{
		}

		public SpriteModel(SpriteViewModel parent) : this()
		{
			this.Parent = parent;
		}
		#endregion

		#region Properties
		public int Index
		{
			get
			{
				return this.index;
			}
			set
			{
				if (this.index != value)
				{
					this.index = value;
					OnPropertyChanged("Index");
					OnPropertyChanged("CanRemove");
				}
			}
		}

		public bool CanRemove
		{
			get
			{
				//Only if there is more than one item in list
				return !(this.parent == null || this.parent.Sprites == null || this.parent.Sprites.Count <= 1);
			}
		}

		public string Name
		{
			get
			{
				return this.name;
			}
			set
			{
				if (this.name != value)
				{
					this.name = value;
					OnPropertyChanged("Name");
				}
			}
		}

		public string Content
		{
			get
			{
				return this.content;
			}
			set
			{
				if (this.content != value)
				{
					this.content = value;
					OnPropertyChanged("Content");
				}
			}
		}

		public string Template
		{
			get
			{
				if (this.template == null)
				{
					this.template = this.TemplateList.FirstOrDefault();
				}
				return this.template;
			}
			set
			{
				if (this.template != value)
				{
					this.template = value;
					OnPropertyChanged("Template");
				}
			}
		}

		public ObservableCollection<string> TemplateList
		{
			get
			{
				return (this.parent != null ? this.parent.TemplateList : null) ?? new ObservableCollection<string>();
			}
		}

		public ICommand AddSpriteCommand
		{
			get
			{
				return this.addSpriteCommand;
			}
			set
			{
				if (this.addSpriteCommand != value)
				{
					this.addSpriteCommand = value;
					OnPropertyChanged("AddSpriteCommand");
				}
			}
		}

		public ICommand RemoveSpriteCommand
		{
			get
			{
				return this.removeSpriteCommand;
			}
			set
			{
				if (this.removeSpriteCommand != value)
				{
					this.removeSpriteCommand = value;
					OnPropertyChanged("RemoveSpriteCommand");
				}
			}
		}

		public SpriteViewModel Parent
		{
			get
			{
				return this.parent;
			}
			set
			{
				if (this.parent != value)
				{
					this.parent = value;
					this.parent.PropertyChanged += (sender, e) =>
					{
						if (e.PropertyName == "TemplateList")
						{
							OnPropertyChanged(e.PropertyName);
						}
					};
					this.AddSpriteCommand = new AddSpriteCommand(this, value);
					this.RemoveSpriteCommand = new RemoveSpriteCommand(this, value);

					OnPropertyChanged("Parent");
					OnPropertyChanged("TemplateList");
				}
			}
		}
		#endregion
	}
}
